<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Userscorelog extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\UserScoreLog();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {

        $pageSize = input('pagesize', 15);
        $userId = $this->auth->id;

        $record_type = input('record_type', 0);
        if (!in_array($record_type, [0, 1])) {
            $this->error('参数非法！');
        }

        $msg = $record_type == 1 ? '获取记录' : '消费记录';
        $where = [
            'user_id' => $userId,
            'record_type' => $record_type
        ];

        $order = input('order', 'createtime');
        $sort = input('sort', 'desc');

        $list = self::$model->where($where)->order($order . ' ' . $sort)->paginate($pageSize);

        $this->success($msg, $list);
    }

    /**
     * 发布图文或者视频
     */
    public function create()
    {
        $type = input('type', 1);//1为图文，2为视频
        $title = input('title');
        $video_url = input('video_url');
        $pics = input('pics');
        $ground_types = input('ground_types');
        $content = input('content');
        $address = input('address');
        $longitude = input('longitude');
        $latitude = input('latitude');

        if (!in_array($type, [1, 2])) {
            $this->error('参数非法！');
        }

        if (!$ground_types) {
            $this->error('必须选择发帖类型！');
        }

        if (!$content) {
            $this->error('必须输入内容！');
        }

        if (!$address) {
            $this->error('必须选择具体地址！');
        }

        if (!$longitude || !$latitude) {
            $this->error('必须上传经纬度！');
        }

        $ddata = [
            'user_id' => $this->auth->id,
            'ground_types' => $ground_types,
            'content' => $content,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
        ];

        switch ($type) {
            case 1:
                if (!$title) {
                    $this->error('请输入标题！');
                }
                if (!$pics) {
                    $this->error('请上传图片！');
                }
                $ddata['title'] = $title;
                $ddata['pics'] = $pics;
                break;
            case 2:
                if (!$video_url) {
                    $this->error('请上传视频！');
                }
                $ddata['video_url'] = $video_url;
                break;
        }

        $ret = \app\common\model\UserDynamics::create($ddata);
        if (!$ret->id) {
            $this->error('发布失败！');
        }

        $this->success('发布成功！');
    }
}
